#include "../stdafx.h"
#include "ddslib.h"
#include <memory.h>

static unsigned char * pBuffer = 0;
static unsigned char * pPointer = 0;

void read_data(void * pDst,unsigned int size)
{
	if(pPointer)
	{
		memcpy(pDst, pPointer, size);
		pPointer += size;
	}
}
void set_dds_data(unsigned char * pData)
{
	pPointer = pBuffer = pData;
	pPointer += 4;
}
void read_dds_surface_info(DDSURFACEDESC2x * pSurfaceDesc)
{
	read_data(pSurfaceDesc,  sizeof(DDSURFACEDESC2x));
}

void read_dds_image(unsigned char ** ppOut, dds_image_info * pInfo)
{
	pInfo->dwByteNum = pInfo->dwWidth * pInfo->dwHeight * 4;
	*ppOut = new unsigned char[pInfo->dwByteNum];
	unsigned char * DestData = *ppOut;
	unsigned char TmpData[4];

	if(pInfo->dwType == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT)
	{
		int index=0;
		for(unsigned int j=0;j<(pInfo->dwHeight);j++)
		{
			for(unsigned int i=0;i<(pInfo->dwWidth);i++)
			{
				if(pInfo->dwAlphaBitDepth==0)
					fetch_2d_texel_rgb_dxt1((pInfo->dwWidth),pPointer,i,j,TmpData);
				else
					fetch_2d_texel_rgba_dxt1((pInfo->dwWidth),pPointer,i,j,TmpData);
				DestData[index]=TmpData[2];
				DestData[index+1]=TmpData[1];
				DestData[index+2]=TmpData[0];
				DestData[index+3]=TmpData[3];
				index+=4;
			}
		}
	}
	else if(pInfo->dwType == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT)
	{
		int index=0;
		for(unsigned int j=0;j<(pInfo->dwHeight);j++)
		{
			for(unsigned int i=0;i<(pInfo->dwWidth);i++)
			{
				fetch_2d_texel_rgba_dxt3((pInfo->dwWidth),pPointer,i,j,TmpData);
				DestData[index]=TmpData[2];
				DestData[index+1]=TmpData[1];
				DestData[index+2]=TmpData[0];
				DestData[index+3]=TmpData[3];
				index+=4;
			}
		}
	}
	else if(pInfo->dwType == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
	{
		int index=0;
		for(unsigned int j=0;j<(pInfo->dwHeight);j++)
		{
			for(unsigned int i=0;i<(pInfo->dwWidth);i++)
			{
				fetch_2d_texel_rgba_dxt5((pInfo->dwWidth),pPointer,i,j,TmpData);
				DestData[index]=TmpData[2];
				DestData[index+1]=TmpData[1];
				DestData[index+2]=TmpData[0];
				DestData[index+3]=TmpData[3];
				index+=4;
			}
		}
	}
	else if(pInfo->dwType == 0)
	{
		int index=0;
		for(unsigned int j=0;j<(pInfo->dwHeight);j++)
		{
			for(unsigned int i=0;i<(pInfo->dwWidth);i++)
			{
				if(pInfo->dwMipFactor ==2)
				{
					unsigned short ColorData;
					memcpy(&ColorData,&pPointer[(j*(pInfo->dwWidth)+i)*pInfo->dwMipFactor],pInfo->dwMipFactor);
					if(pInfo->dwGBitMask==0x3e0)
					{
						//CColorConverter::Convert_A1R5G5B5toA8R8G8B8(&ColorData,1,&DestData[index]);
						DestData[index+3]=0xff;
					}
					/*else
						CColorConverter::Convert_R5G6B5toA8R8G8B8(&ColorData,1,&DestData[index]);*/
				}
				else if(pInfo->dwMipFactor==3)
				{
					DestData[index]=pPointer[(j*(pInfo->dwWidth)+i)*pInfo->dwMipFactor];
					DestData[index+1]=pPointer[(j*(pInfo->dwWidth)+i)*pInfo->dwMipFactor+1];
					DestData[index+2]=pPointer[(j*(pInfo->dwWidth)+i)*pInfo->dwMipFactor+2];
					DestData[index+3]=0xff;
				}
				else if(pInfo->dwMipFactor==4)
				{
					if(pInfo->dwRBitMask>0xff)
					{
						DestData[index]=pPointer[index];
						DestData[index+1]=pPointer[index+1];
						DestData[index+2]=pPointer[index+2];
						if(pInfo->dwRGBAlphaBitMask!=0)
							DestData[index+3]=pPointer[index+3];
						else
							DestData[index+3]=0xff;
					}
					else
					{
						DestData[index]=pPointer[index+2];
						DestData[index+1]=pPointer[index+1];
						DestData[index+2]=pPointer[index];
						if(pInfo->dwRGBAlphaBitMask!=0)
							DestData[index+3]=pPointer[index+3];
						else
							DestData[index+3]=0xff;

					}
				}
				else
				{
					*ppOut = 0;
					return;
				}
				index+=4;
			}
		}
	}
}